package com.ww.dao.impl;


import com.ww.dao.TrolleyDao;
import com.ww.entity.Trolley;
import com.ww.util.DBUtil;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.*;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;


/**
 * 购物车表dao层实现类
 */
public class TrolleyDaoImpl implements TrolleyDao {
    private Connection conn = DBUtil.getConnection();
    private QueryRunner qr = new QueryRunner();
    @Override
    public int save(Trolley obj) {
        String sql = "INSERT INTO  tb_trolley(trolley_id,user_id,goods_id,delivery_id,trolley_num,trolley_status) VALUES (?,?,?,?,?,?)";
        Object[] paras = {obj.getTrolley_id(),obj.getUser_id(),obj.getGoods_id(),obj.getDelivery_id()
        ,obj.getTrolley_num(),obj.getTrolley_status()};
        try {
            int row = qr.update(conn, sql, paras);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int update(Trolley obj) {
        String sql = "INSERT INTO  tb_trolley(trolley_id,user_id,goods_id,delivery_id,trolley_num,trolley_status) VALUES (?,?,?,?,?,?)";
        Object[] paras = {obj.getUser_id(),obj.getGoods_id(),obj.getDelivery_id()
                ,obj.getTrolley_num(),obj.getTrolley_status(),obj.getTrolley_id()};
        try {
            int row = qr.update(conn, sql, paras);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int delete(Object id) {
        String sql = "DELETE FROM tb_trolley WHERE trolley_id  = ?";
        Object[] paras = {id};
        try {
            int row = qr.update(conn, sql, paras);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Trolley findById(Object id) {
        String sql = "SELECT * FROM tb_trolley where trolley_id  = ?";
        Object[] paras = {id};
        try {
            Trolley trolley = qr.query(conn, sql, paras, new BeanHandler<Trolley>(Trolley.class));
            return trolley;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Trolley> findAll() {
        String sql = "SELECT * FROM tb_trolley";
        Object[] paras = {};
        try {
            List<Trolley> goodsList = qr.query(conn, sql, paras, new BeanListHandler<Trolley>(Trolley.class));
            return goodsList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Trolley> findSearch(String keyword) {
        return null;
    }

    @Override
    public List<Trolley> findPage(int page, int pagesize) {
        String sql = "SELECT * FROM tb_trolley LIMIT ?,?";
        Object[] paras = {(page-1)*pagesize,pagesize};
        try {
            List<Trolley> goodsList = qr.query(conn, sql, paras, new BeanListHandler<Trolley>(Trolley.class));
            return goodsList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public long count() {
        String sql = "select count(*) from tb_trolley";
        Object[] paras = {};
        try {
            long nums = qr.query(conn, sql, paras, new ScalarHandler<Long>());
            return nums;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public List<Map<String, Object>> getUserAndGoods(Object id) {
        String sql = "SELECT g.goods_id,g.goods_name,g.goods_price,g.goods_date,g.goods_img,\n" +
                "u.user_id,u.user_name,t.trolley_num,t.trolley_id FROM tb_trolley t,tb_goods g,tb_user u where\n" +
                "t.goods_id = g.goods_id AND t.user_id = u.user_id AND t.user_id = ?";
        Object [] para = {id};
        try {
            List<Map<String,Object>> mapList = qr.query(conn,sql,para,new MapListHandler());
            return mapList;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int updateTrolley(String id,int number) {
        String sql = "UPDATE tb_trolley SET trolley_num = ? WHERE trolley_id = ?";
        Object[] paras = {number,id};
        try {
            int row = qr.update(conn, sql, paras);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Trolley isRepeat(String user_id,String goods_id) {
        String sql = "SELECT * FROM tb_trolley WHERE user_id = ? AND goods_id = ?";
        Object[] paras = {user_id,goods_id};
        try {
            Trolley trolley = qr.query(conn, sql, paras, new BeanHandler<Trolley>(Trolley.class));
            return trolley;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int deletaT(String user_id, String goods_id) {
        String sql = "DELETE FROM tb_trolley WHERE user_id = ? AND goods_id = ?";
        Object[] paras = {user_id,goods_id};
        try {
            int row = qr.update(conn, sql, paras);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int remove(String id) {
        String sql = "DELETE FROM tb_trolley WHERE trolley_id in ?";
        Object[] paras = {id};
        try {
            int row = qr.update(conn, sql, paras);
            return row;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public Map<String, Object> find(String id) {
        String sql = "SELECT g.goods_id,g.goods_name,g.goods_price,g.goods_date,g.goods_img,u.user_id,u.user_name,t.trolley_num,t.trolley_id FROM tb_trolley t,tb_goods g,tb_user u where t.goods_id = g.goods_id AND t.user_id = u.user_id AND trolley_id = ?";
        Object[] paras = {id};

        try {
            Map<String,Object> map = qr.query(conn,sql,paras,new MapHandler());
            return map;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
